using System;

namespace Common.Utilities.Persistence
{
    /// <summary>
    /// 
    /// </summary>
    public class FieldValueConverter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbType"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertToString(System.Data.SqlDbType dbType, object value)
        {
            if (value == null)
                return "null";

            string strValue = string.Empty;
            switch (dbType)
            {
                case System.Data.SqlDbType.BigInt:
                case System.Data.SqlDbType.Decimal:
                case System.Data.SqlDbType.Float:
                case System.Data.SqlDbType.Int:
                case System.Data.SqlDbType.Money:
                case System.Data.SqlDbType.Real:
                case System.Data.SqlDbType.SmallInt:
                case System.Data.SqlDbType.SmallMoney:
                case System.Data.SqlDbType.TinyInt:
                    strValue = value.ToString();
                    break;
                case System.Data.SqlDbType.Char:
                case System.Data.SqlDbType.VarChar:
                case System.Data.SqlDbType.NChar:
                case System.Data.SqlDbType.NVarChar:
                    strValue = value.ToString();
                    break;
                case System.Data.SqlDbType.NText:
                case System.Data.SqlDbType.Text:
                    strValue = value.ToString();
                    break;
                case System.Data.SqlDbType.Bit:
                    strValue = "0";
                    bool boolValue = bool.Parse(value.ToString());
                    if (boolValue)
                        strValue = "1";
                    break;
                case System.Data.SqlDbType.DateTime:
                case System.Data.SqlDbType.SmallDateTime:
                    strValue = value.ToString();
                    break;
                case System.Data.SqlDbType.Binary:
                case System.Data.SqlDbType.Image:
                case System.Data.SqlDbType.VarBinary:
                    byte[] bytes = value as byte[];
                    if (bytes != null)
                        strValue = Base64Util.EncodeAsBase64(bytes);
                    break;
                default:
                    break;

            }
            strValue = strValue.Replace("'", "''");
            return strValue;
        }

        ///<summary>
        ///</summary>
        ///<param name="dbType"></param>
        ///<param name="value"></param>
        ///<returns></returns>
        public static object ToValue(System.Data.SqlDbType dbType, string value)
        {
            object objValue = null;
            if (value != null)
            {
                switch (dbType)
                {
                    case System.Data.SqlDbType.BigInt:
                        objValue = Int64.Parse(value);
                        break;
                    case System.Data.SqlDbType.Decimal:
                        objValue = decimal.Parse(value);
                        break;
                    case System.Data.SqlDbType.Float:
                        objValue = float.Parse(value);
                        break;
                    case System.Data.SqlDbType.Int:
                        objValue = int.Parse(value);
                        break;
                    case System.Data.SqlDbType.Money:
                        objValue = decimal.Parse(value);
                        break;
                    case System.Data.SqlDbType.Real:
                        objValue = double.Parse(value);
                        break;
                    case System.Data.SqlDbType.SmallInt:
                        objValue = Int16.Parse(value);
                        break;
                    case System.Data.SqlDbType.SmallMoney:
                        objValue = decimal.Parse(value);
                        break;
                    case System.Data.SqlDbType.TinyInt:
                        objValue = Int16.Parse(value);
                        break;
                    case System.Data.SqlDbType.Char:
                    case System.Data.SqlDbType.VarChar:
                    case System.Data.SqlDbType.NChar:
                    case System.Data.SqlDbType.NVarChar:
                        objValue = value;
                        break;
                    case System.Data.SqlDbType.NText:
                    case System.Data.SqlDbType.Text:
                        objValue = value;
                        break;
                    case System.Data.SqlDbType.Bit:
                        if (value == "0")
                            objValue = false;
                        else if (value == "1")
                            objValue = true;
                        else
                            objValue = false;
                        break;
                    case System.Data.SqlDbType.DateTime:
                    case System.Data.SqlDbType.SmallDateTime:
                        objValue = DateTime.Parse(value);
                        break;
                    case System.Data.SqlDbType.Binary:
                    case System.Data.SqlDbType.Image:
                    case System.Data.SqlDbType.VarBinary:
                        byte[] contents = Base64Util.DecodeAsBytes(value);
                        objValue = contents;
                        break;
                    default:
                        break;

                }
            }
            return objValue;
        }
    }
}
